package com.nes.springcloud.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;


@Component
public class MyFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(MyFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("accessToken");
        if(accessToken == null) {
            throw new RuntimeException("xxxx");
//            log.warn("accessToken is empty");
//            ctx.setSendZuulResponse(false);//令zuul过滤该请求，不对其进行路由
//            ctx.setResponseStatusCode(401);//设置了其返回的错误码
//            try {
//                ctx.getResponse().getWriter().write("accessToken is empty");
//            }catch (Exception e){}
//
//            return null;
        }
        log.info("access ok");
        return null;
    }
}